/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package akheer.safe.services;

import akheer.safe.SMS;
import akheer.safe.net.RabbitClient;
import akheer.safe.net.RabbitClientMessage;

import java.io.*;
import java.util.logging.Level;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 *
 * @author saaduddin.ahmad
 */
public class EmailService implements Observer {

    private static final Logger log = LoggerFactory.getLogger("ES");

    private RabbitClient rc;

    private boolean isRunning;

    public EmailService() throws IOException {

        isRunning = false;

        init();
    }

    private void init() throws IOException {

        this.isRunning = false;

        rc = new RabbitClient("FS", "fsIN", "fsIN");
        rc.addObserver(this);
    }

    public void start() {

        this.isRunning = false;
        
        rc.startListening("fsIN", false);
    }

    public void stop() {

        rc.stopListening();

        this.isRunning = false;
    }
    
    public void update(Observable o, Object arg) {

        try {
            
            RabbitClientMessage rcmIn = (RabbitClientMessage)arg;

            SMS sms = new SMS(rcmIn.getMessage().toString());
            String fileData = "To: " + sms.getReceivingNumber()
            //String fileData = "To: " + sms.getSendingNumber()
                    + "\r\n"
                    + "\r\n"
                    + sms.getTextMessage();

            int c=0;
            String[] to = new String[4];
            to[c++] = "madeelhasan@yahoo.com";
            to[c++] = "saaduddin.ahmad@pmex.com.pk";
            to[c++] = "s.agahi@gmail.com";
            to[c++] = "uz.jaswal@gmail.com";

            for( int i=0; i < to.length; i++) {
                send("10.1.3.60", to[i], "saaduddin.ahmad@pmex.com.pk" , "naml", fileData);
            }
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    private void send(String smtpServer, String to, String from , String subject, String body)
    {
        try
        {
            Properties props = System.getProperties();
            // -- Attaching to default Session, or we could start a new one --
            props.put("mail.smtp.host", smtpServer);
            Session session = Session.getDefaultInstance(props, null);
            // -- Create a new message --

            Message msg = new MimeMessage(session);
            // -- Set the FROM and TO fields --
            msg.setFrom(new InternetAddress(from));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));


            // -- We could include CC recipients too --
            // if (cc != null)
            // msg.setRecipients(Message.RecipientType.CC
            // ,InternetAddress.parse(cc, false));
            // -- Set the subject and body text --
            msg.setSubject(subject);
            msg.setText(body);
            // -- Set some other header information --
            msg.setHeader("X-Mailer", "LOTONtechEmail");
            msg.setSentDate(new Date());
            // -- Send the message --
            Transport.send(msg);
            System.out.println("Message sent OK.");
        }
       catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    public static void main(String[] args) {

        EmailService es = null;
        try {
            es = new EmailService();
            es.start();
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(EmailService.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
